package com.market.service.impl;

import com.market.common.exception.ServiceException;
import com.market.common.vo.ResponseCode;
import com.market.entity.*;
import com.market.mapper.BrandMapper;
import com.market.mapper.ClassesMapper;
import com.market.mapper.CommodityMapper;
import com.market.mapper.UnitsMapper;
import com.market.service.CommodityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommodityServiceImpl implements CommodityService {
    @Resource
    CommodityMapper commodityMapper;
    @Resource
    ClassesMapper classesMapper;
    @Resource
    BrandMapper brandMapper;
    @Resource
    UnitsMapper unitsMapper;

    @Override
    public int addCommodity(Commodity commodity) throws ServiceException {
        boolean classesFlag = false;
        boolean brandFlag = false;
        boolean unitsFlag = false;
        /**
         * 判断类名是否存在
         */
        List<Classes> classes = classesMapper.selectNames();
        for (Classes classess : classes) {
            if (classess.getCommodityClassesName().equals(commodity.getCommodityClassesName())) {
                classesFlag = true;
            }
        }
        /**
         * 判断品牌是否存在
         */
        List<Brand> brands = brandMapper.selectNames();
        for (Brand brand : brands) {
            if (brand.getCommodityBrandName().equals(commodity.getCommodityBrandName())) {
                brandFlag = true;
            }
        }
        /**
         * 判断单位名称是否存在
         */
        List<Units> units = unitsMapper.selectNames();
        for (Units unitss : units) {
            if (unitss.getCommodityUnitsName().equals(commodity.getCommodityUnitsName())) {
                unitsFlag = true;
            }
        }

        if (classesFlag && brandFlag && unitsFlag) {
            int insers = commodityMapper.insers(commodity);
            if (insers > 0) {
                throw new ServiceException(ResponseCode.SUCCESS);
            } else {
                throw new ServiceException(ResponseCode.ERROR);
            }
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    /**
     * 删除
     * @param commodityId
     * @return
     * @throws ServiceException
     */
    @Override
    public int deleteCommdity(Integer commodityId) throws ServiceException{
        int i = commodityMapper.deleteBrand(commodityId);
        if (i > 0){
            throw   new ServiceException(ResponseCode.SUCCESS);
        }else {
            throw   new ServiceException(ResponseCode.ERROR);
        }
    }

    /**
     * chax
     * @return
     * @throws ServiceException
     */
    @Override
    public List<Commodity> findCommodity() throws ServiceException{
        List<Commodity> selects = commodityMapper.selects();
        if (selects.size() > 0) {
            return selects;
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }
}
